package com.polly.esphwevent.controller;

import com.polly.esphwevent.event.AssessmentPublishEvent;
import com.polly.esphwevent.event.AssessmentRespondenceEvent;
import com.polly.esphwevent.common.disruptor.HomeworkEventProducer;
import com.polly.esphwevent.domain.HomeworkInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @title: AssigningController
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2019/2/11 14:35
 * @author ldz112@163.com
 * @version V1.0
 */

@RestController
public class AssigningController{

    @Autowired
    private HomeworkEventProducer homeworkEventProducer;


    @RequestMapping(value = "/drafts/{homework_id}", method = RequestMethod.POST)
    public HomeworkInfo drafts( @PathVariable(value = "homework_id") String homeworkId,@RequestBody HomeworkInfo hwInfo) {

        hwInfo.setHomeworkId(UUID.randomUUID().toString()+"@"+homeworkId);
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {

        }

        homeworkEventProducer.publishEvent(new AssessmentPublishEvent(hwInfo.getHomeworkId(),1L));
        return hwInfo;
    }

    @RequestMapping(value = "/homeworks/{homework_id}", method = RequestMethod.GET)
    public String homeworks( @PathVariable(value = "homework_id") String homeworkId) {

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {

        }

        homeworkEventProducer.publishEvent(new AssessmentRespondenceEvent(homeworkId,1L));
        return homeworkId;
    }

}
